package dao;

import basetest.Employee;
import org.junit.Test;

import java.sql.Connection;
import java.sql.Timestamp;

/**
 * @author DuJiabao
 * @create 2021-01-16 12:45
 */
public class EmployeeDAO extends BaseDAO<Employee> implements EmployeeDAOInterface {
    @Test
    public void test() {
        Connection conn = null;
        try {
            conn = getConnection();
            Employee john = new Employee(1001, "John", "Cena", "john@163.com", "413413552",
                    "AC_ACCOUNT", 12000, 0.1, 146, 80,
                    new Timestamp(5245245245L));
            int update = update(conn, john);
            System.out.println(update);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeSource(conn);
        }
    }

    @Override
    public int update(Connection conn, Employee employee) {
        String sql = "update new_table2 set employee_id=?, first_name=?, last_name=?, email=?, phone_number=?, job_id=?," +
                "salary=?,commission_pct=?, manager_id=?, department_id=?, hiredate=?" +
                "where employee_id=?";
        return update(conn, sql, employee.getEmployee_id(), employee.getFirst_name(), employee.getLast_name(), employee.getEmail(),
                employee.getPhone_number(), employee.getJob_id(), employee.getSalary(), employee.getCommission_pct(),
                employee.getManager_id(), employee.getDepartment_id(), employee.getHiredate(), employee.getEmployee_id());
    }

    @Override
    public void insert(Connection conn, Employee employee) {
        String sql = "insert into new_table2 values(?,?,?,?,?,?,?,?,?,?,?)";
        update(conn, sql, employee.getEmployee_id(), employee.getFirst_name(), employee.getLast_name(), employee.getEmail(),
                employee.getPhone_number(), employee.getJob_id(), employee.getSalary(), employee.getCommission_pct(),
                employee.getManager_id(), employee.getDepartment_id(), employee.getHiredate());
    }

    @Override
    public void deleteById(Connection conn, int employee_id) {
        String sql = "delete from new_table2 where employee_id=?";
        update(conn, sql, employee_id);
    }
}
